require "battle"

class BattleController < ApplicationController
  @game_battles = {}

  def list
    @battles = []

    @game_battles.each do |b|
      @battles << { :user_name => b.user_name,
                    :wating_time => (Time.now.to_i - b.start_time),
                    :id => b.id }
    end

    render
  end

  def join
    battle = @game_battles[params[:id]]
    battle.join(current_user)

    session[:battle_id] = battle.id

    redirect_to :index, :id => battle.id
  end

  def create
    battle = Battle.new
    battle.id = @game_battles.size
    battle.join(current_user)

    @game_battles << battle
  end

  def step
    x = params[:x]
    y = params[:y]
    battle = @game_battles[session[:battle_id]]
    winner = battle.step(current_user.id,x,y)

    if winner == current_user.id and winner != 0
      redirect_to :list, :notice => "You win!!!"
    elsif winner != 0
      redirect_to :list, :notice => "You lose!!!"
    end
  end

  def index
    @battle = @game_battles[params[:id]]


    render
  end

  def disconnect
  end

end
